#include "TKSPStdAfx.h"

#include "DBXDataGetter.h"
#include "DBXDeviceManager.h"

#include <libconfig.hpp>

namespace DBX
{
    namespace Constants
    {
        extern const char *keyDataLabel;
        extern const char *keyDataType;
        extern const char *keyDataValue;
    } // namespace Constants

    class DBXDataGetter::Private
    {
    public:
        Private(DBXDataGetter *p)
            : o(p)
        {}

        void get()
        {
            Setting &setting = o->getRoot();
            auto count = setting.getLength();
            for(int i = 0; i < count; ++i) {
                auto &item = setting[i];
                std::string label;
                int dataType = -1;
                if(!(item.lookupValue(Constants::keyDataLabel, label) &&
                     item.lookupValue(Constants::keyDataType, dataType))) {
                    continue;
                }
                auto pos = label.find_first_of(".");
                if(pos != std::string::npos) {
                    if(auto manager = DBXDeviceManager::instance()) {
                        if(auto device = manager->find(label.substr(pos))) {
                            device->get(item);
                        }
                    }
                }
            }
        }

    private:
        friend class DBXDataGetter;
        DBXDataGetter *o;
    };

    DBXDataGetter::DBXDataGetter()
        : p(*new Private(this))
    {}

    DBXDataGetter::~DBXDataGetter()
    {
        delete &p;
    }

    void DBXDataGetter::get()
    {
        p.get();
    }
} // namespace DBX